package cn.jbolt.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Component
public class FilesUtil {

    //分割符号
    public static String splitSymbol = "<!--------------------------------------------------------------------------------------------!>";

    private static final Logger logger = LoggerFactory.getLogger(FilesUtil.class);


    /**
     * 创建文件
     *
     * @param
     * @param fileName
     */
    public static Result<File> createFileInFolder(String fileName) {
        // 确保文件夹存在，不存在则创建
        String s = System.getProperty("user.dir") + "/tmp/file";
        File folder = FileUtil.mkdir(s);
        // 创建文件对象，指定文件路径（包含文件名）
        File file = FileUtil.file(folder, fileName);
        try {
            // 创建文件，如果文件已存在则返回false，创建成功返回true
            File touch = FileUtil.touch(file);
            if (touch != null) {
                logger.info("文件创建成功: " + file.getAbsolutePath());
                return Result.success(file);
            } else {
                logger.error("文件已存在或创建失败: " + file.getAbsolutePath());
                return Result.error("文件已存在或创建失败");
            }
        } catch (Exception e) {
            logger.error("文件创建失败: " + file.getAbsolutePath(), e);
            return Result.error("文件创建失败");
        }
    }

    /**
     * 移除文件
     *
     * @param filePath
     * @return
     */
    public static Result deleteFile(String filePath) {
        File file = FileUtil.file(filePath);
        boolean deleted = FileUtil.del(file);
        if (deleted) {
            logger.info("文件删除成功: " + filePath);
            return Result.success();

        } else {
            logger.error("文件不存在或删除失败: " + filePath);
            return Result.error("文件不存在或删除失败");
        }
    }

    /**
     * 根据文件地址,文件分隔符,把内容返回一个list数组
     *
     * @param filePath
     * @param delimiter
     * @return
     */
    public static List<String> readAndSplitFile(String filePath, String delimiter) {
        List<String> resultList = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 根据指定的符号进行分割，这里以逗号为例，你可以替换为实际的delimiter
                String[] parts = line.split(delimiter);
                for (String part : parts) {
                    resultList.add(part.trim()); // 添加分割后的元素到列表，去除两端空白字符
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultList;
    }




    public static void main(String[] args) {
        String networkUrl = "http://localhost:8013/vector/250312/Mo1YJd/HTML.xlsx";
        try (InputStream inputStream = new URL(networkUrl).openStream()) {
            // 使用Hutool通过输入流读取Excel文件
            ExcelReader reader = ExcelUtil.getReader(inputStream);
            System.out.println(reader.read());
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
